package com.lemon.utils.util;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.cache.annotation.EnableCaching;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.data.redis.connection.RedisConnectionFactory;  
import org.springframework.data.redis.core.*;  
  
/** 
 * Created by Administrator on 2017/7/2. 
 * Redis模板配置，缓存配置 
 */  
  
@Configuration  
@EnableCaching  
public class RedisConfig {  
  
    @Autowired  
    RedisConnectionFactory redisConnectionFactory;  
  
    /** 
     * 实例化 RedisTemplate 对象 
     */  
    @Bean  
    public RedisTemplate<String, Object> functionDomainRedisTemplate() {  
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();  
        initDomainRedisTemplate(redisTemplate, redisConnectionFactory);  
        return redisTemplate;  
    }  
  
    /** 
     * 设置数据存入 redis 的序列化方式 
     */  
    private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {  
        /*redisTemplate.setKeySerializer(new StringRedisSerializer()); 
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); 
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); 
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(Object.class));*/  
        redisTemplate.setConnectionFactory(factory);  
    }  
  
    /** 
     * 实例化 HashOperations 对象,可以使用 Hash 类型操作 
     */  
    @Bean  
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {  
        return redisTemplate.opsForHash();  
    }  
  
    /** 
     * 实例化 ValueOperations 对象,可以使用 String 操作 
     */  
    @Bean  
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {  
        return redisTemplate.opsForValue();  
    }  
  
    /** 
     * 实例化 ListOperations 对象,可以使用 List 操作 
     */  
    @Bean  
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {  
        return redisTemplate.opsForList();  
    }  
  
    /** 
     * 实例化 SetOperations 对象,可以使用 Set 操作 
     */  
    @Bean  
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {  
        return redisTemplate.opsForSet();  
    }  
  
    /** 
     * 实例化 ZSetOperations 对象,可以使用 ZSet 操作 
     */  
    @Bean  
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {  
        return redisTemplate.opsForZSet();  
    }  
      
}  